package org.alex.samples.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.alex.samples.model.CommonMessageResultVO;
import org.alex.samples.model.ExamScoreVO;
import org.alex.samples.model.ExamSubjectVO;
import org.alex.samples.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/teacher")
public class TeacherController {

	//@Resource(name="teacherService")
	@Autowired
	private ITeacherService teacherService;
	
	//查询全部科目
	@RequestMapping(value="/toManageSubjects")
	public String toManageSubjects(HttpSession session,Model model){
		List<ExamSubjectVO> examSubjectList=teacherService.getAllSubjects();
		model.addAttribute("examSubjectList",examSubjectList);
		return "teacher/manageSubjects";
	}
	
	
	//管理成绩
	@RequestMapping(value = "/toManageScore")
	public String toManageScore(Model model){
		List<ExamScoreVO> scoreList = teacherService.getScoreList();
		model.addAttribute("scoreList", scoreList);
		return "teacher/manageScore";
	}
	
	
	
	
	// 获取所有当前科目关联的学生的后台方法
	@ResponseBody
	@RequestMapping(value="getAllStudent")
	public Map<String,Object> getAllStudent(Integer subjectId){
		Map<String,Object> resultMap=new HashMap<String,Object>();
		resultMap.put("allUser",teacherService.getAllStudent());//获取所有学生，包括没关联的
		resultMap.put("selectedUser",teacherService.getAllUserBySubject(subjectId));//获取所有已关联的学生
		//System.out.println(teacherService.getAllUserBySubject(subjectId));
		return resultMap;
	}
	
	
	//保存修改 管理科目学生
		@ResponseBody
		@RequestMapping(value="saveSubjectStudents")
		public Map<String,Object> saveSubjectStudents(@RequestParam(name="subjectStudents[]")String[] subjectStudents,Integer subjectId ){
			Map<String,Object> resultMap=new HashMap<String,Object>();
			resultMap.put("success",false);
			List<String> list=new ArrayList<String>();
			for(String student : subjectStudents){
				list.add(student);
			}
			try{
				//传到service，有service来处理
				teacherService.saveSubjectStudent(list,subjectId);
				resultMap.put("success", true);
			}catch(Exception e){
				e.printStackTrace();
			}
			return resultMap;
		}
		
		
		//新增科目信息
		@ResponseBody
		@RequestMapping(value="/saveSubject",method=RequestMethod.POST)
		public CommonMessageResultVO saveTest(@Valid ExamSubjectVO examSubjectVO,BindingResult bindingResult){
			CommonMessageResultVO resultVO=new CommonMessageResultVO();
			Map<String,String> messageMap=new HashMap<String,String>();
			resultVO.setSuccess(false);
			if(bindingResult.hasErrors()){
				List<ObjectError> errorList=bindingResult.getAllErrors();
				for(ObjectError error:errorList){
					FieldError fe=(FieldError) error;
					messageMap.put(fe.getField(),fe.getDefaultMessage());
				}
				resultVO.setMessageMap(messageMap);
				return resultVO;
			}
			
			try{
				teacherService.addSubject(examSubjectVO);
				resultVO.setSuccess(true);
			}catch(Exception e){
				e.printStackTrace();
				messageMap.put("message", e.getMessage());
				resultVO.setMessageMap(messageMap);
			}
			return resultVO;
		}
		
		
		//修改科目 --查看科目信息
		@ResponseBody
		@RequestMapping(value="/getSubjectById",method = RequestMethod.POST)
		public ExamSubjectVO getSubjectById(Integer subId){
			ExamSubjectVO resultVO=teacherService.getSubjectById(subId);
			return resultVO;
		}
		
		
		//修改科目
		@ResponseBody
		@RequestMapping(value="/updateSubject",method=RequestMethod.POST)
		public CommonMessageResultVO updateSubject(@Valid ExamSubjectVO examSubjectVO,BindingResult bindingResult){
			CommonMessageResultVO resultVO=new CommonMessageResultVO();
			Map<String,String> messageMap=new HashMap<String,String>();
			resultVO.setSuccess(false);
			if(bindingResult.hasErrors()){
				List<ObjectError> errorList=bindingResult.getAllErrors();
				for(ObjectError error:errorList){
					FieldError fe=(FieldError) error;
					messageMap.put(fe.getField(),fe.getDefaultMessage());
				}
				resultVO.setMessageMap(messageMap);
				return resultVO;
			}
			
			try{
				teacherService.editSubject(examSubjectVO);
				resultVO.setSuccess(true);
			}catch(Exception e){
				e.printStackTrace();
				messageMap.put("message", e.getMessage());
				resultVO.setMessageMap(messageMap);
			}
			return resultVO;
		}



		//删除科目
			@ResponseBody
			@RequestMapping(value="/deleteSubject",method=RequestMethod.POST)
			public CommonMessageResultVO deleteSubject(Integer subId){
				CommonMessageResultVO resultVO=new CommonMessageResultVO();
				Map<String,String> messageMap=new HashMap<String,String>();
				resultVO.setSuccess(false);
				try{
					teacherService.deleteSubject(subId);
					resultVO.setSuccess(true);
				}catch(Exception e){
					e.printStackTrace();
					messageMap.put("message", e.getMessage());
					resultVO.setMessageMap(messageMap);
				}
				return resultVO;
			}
			
		//按条件查询成绩
			@ResponseBody
			@RequestMapping(value = "/getScore", method = RequestMethod.POST)
			public Map<String, Object> getScore(String condition, String query){
				Map<String, Object> resultMap = new HashMap<String, Object>();
				resultMap.put("success", false);
				List<ExamScoreVO> resultList;
				try {
					resultList = teacherService.getScore(condition, query);
					if(resultList.size() > 0){
						resultMap.put("resultList", resultList);
						resultMap.put("success", true);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return resultMap;
			}
			
			
	//根据成绩编号查询成绩表数据
	@ResponseBody
	@RequestMapping(value="/getExamScoreById",method = RequestMethod.POST)
	public ExamScoreVO getExamScoreById(Integer id){
		ExamScoreVO resultVO=teacherService.getExamScoreById(id);
		return resultVO;
	}			
		
	
	//修改成绩
	@ResponseBody
	@RequestMapping(value="/updateScore",method=RequestMethod.POST)
	public CommonMessageResultVO updateScore(Integer id, Double score){
		CommonMessageResultVO resultVO=new CommonMessageResultVO();
		Map<String,String> messageMap=new HashMap<String,String>();
		resultVO.setSuccess(false);	
		try{
			/*teacherService.updateScore(examScoreVO);*/
			teacherService.updateScore(id,score);
			resultVO.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}			

	
}
